package com.konsung.network;

import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.konsung.bean.BaseResponse;
import com.konsung.bean.CheckData;
import com.konsung.bean.EcgDiagnoseApplyRequest;
import com.konsung.bean.MeasureDataBean;
import com.konsung.bean.PatientBean;
import com.konsung.bean.QueryEcgDiagnosesRequest;
import com.konsung.bean.QueryHealthFile;
import com.konsung.bean.VillageHealthPortRequest;
import com.konsung.greendaoUtil.DBUtitl;
import com.konsung.greendaoUtil.GreenDaoManager;
import com.konsung.greendaoUtil.PatientBeanDao;
import com.konsung.healthfile.R;
import com.konsung.provide.ProviderReader;
import com.konsung.ui.MyApplication;
import com.konsung.util.BroadcastUtils;
import com.konsung.util.DBDataUtil;
import com.konsung.util.EcgRemoteInfoSaveModule;
import com.konsung.util.JsonUtil;
import com.konsung.util.KParamType;
import com.konsung.util.MeasureUtil;
import com.konsung.util.ToastUtils;
import com.konsung.util.UiUtils;
import com.konsung.util.UnitConvertUtil;
import com.konsung.util.constant.GlobalConstant;
import com.loopj.android.http.TextHttpResponseHandler;
import com.synjones.bluetooth.BmpUtil;

import org.apache.http.Header;
import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * 数据上传
 */

public class UploadData {
    private Context context;
    private final String textStyle = "utf-8";
    private String ip;
    private String port;

    /**
     * 回调接口
     */
    public interface ResponseCallBack {
        /**
         * 成功回调
         * @param s 字符
         */
        void onSuccess(String s);

        /**
         * 失败回调
         * @param message 消息字符
         */
        void onFailure(String message);

        /**
         * 异常回调
         */
        void onException();
    }

    //接口对象
    private ResponseCallBack responseCallBack;

    /**
     * 构造方法
     * @param context 上下文引用
     */
    public UploadData(Context context) {
        this.context = context;
        String path = ProviderReader.getUploadThirdpartyPath(context);
        String exit = ProviderReader.getUploadExitPath(context);
        String[] split = path.split(UiUtils.getString(R.string.colon));
        if (split.length >= 2) {
            URLManage.getInstance().setVillagehealthport(split[0], split[1], exit);
            URLManage.getInstance().setRequestEcgDiagnose(split[0], split[1]);
            URLManage.getInstance().setQueryEcgDiagnoses(split[0], split[1]);
            URLManage.getInstance().setHealthFilePort(split[0], split[1]);
            URLManage.getInstance().setConfigPort(split[0], split[1]);
            URLManage.getInstance().setDownloadPatientUrl(split[0], split[1]);
            URLManage.getInstance().setDownloadHeadUrl(split[0], split[1]);
        }
    }

    /**
     * 测量数据上传
     * @param measureDataBean 测量bean类
     * @param responseCallBack 结果回调接口
     */
    public void uploadMeasureData(final MeasureDataBean measureDataBean
            , final ResponseCallBack responseCallBack) {
        this.responseCallBack = responseCallBack;
        CheckData checkData = ProviderReader.getCheckData(context);
        if (checkData == null) {
            return;
        }

        List<PatientBean> patientBeen = DBUtitl.queryIdCard(context, measureDataBean.getIdcard());
        if (patientBeen != null && patientBeen.size() > 0) {
            final PatientBean patientBean = patientBeen.get(0);
            if (UiUtils.isShuYangArea()) {
                if (!patientBean.getIsFileCreat()) {
                    //针对标记未建档的居民时，先查询该居民是否建档，如果已经建档则标记该居民已经建立档案，并且上传数据，如果未建档，则标记未建档，不上传数据
                    if (UiUtils.isConnectNetWork(MyApplication.getContext())) {
                        final PatientBeanDao patientDao = GreenDaoManager
                                .getPatientDao(context);
                        QueryHealthFile healthFile = new QueryHealthFile();
                        healthFile.setIdnumber(patientBean.getIdcard());
                        healthFile.setSelfname(patientBean.getName());
                        try {
                            StringEntity stringEntity = new StringEntity(JsonUtil
                                    .toJson(healthFile), "utf-8");
                            RequestUtils.clientPost(MyApplication.getContext(),
                                    URLManage.getInstance().healthfileport,
                                    stringEntity, new TextHttpResponseHandler() {

                                        @Override
                                        public void onFailure(int statusCode, Header[] headers,
                                                String responseString, Throwable throwable) {

                                        }

                                        @Override
                                        public void onSuccess(int statusCode, Header[] headers,
                                                String responseString) {
                                            patientBean.setIsOtherNetSuccess(true);
                                            if (responseString != null) {
                                                JsonObject jsonObject = new JsonParser()
                                                        .parse(responseString)
                                                        .getAsJsonObject();
                                                int resultCode = jsonObject.get("resultCode")
                                                        .getAsInt();
                                                if (resultCode == GlobalConstant.HTTP_SUCCESS) {
                                                    //有档案
                                                    patientBean.setIsFileCreat(true);
                                                } else if (resultCode == GlobalConstant
                                                        .HTTP_NO_HAVE) {
                                                    //没有档案

                                                } else {
                                                    //请求失败
                                                    patientBean.setIsOtherNetSuccess(false);
                                                }
                                                patientDao.update(patientBean);
                                                BroadcastUtils.sendRefrshPatient();
                                            }
                                        }
                                    });
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
                //查询之后没建档，不上传数据
                if (!patientBean.getIsFileCreat()) {
                    return;
                }
            }
            VillageHealthPortRequest vhpr = new VillageHealthPortRequest();
            BroadcastUtils.sendOnUploadForAppUpdate();
            fillData(vhpr, measureDataBean, patientBean, checkData);
            try {
                StringEntity stringEntity = new StringEntity(JsonUtil.toJson(vhpr)
                        , textStyle);
                RequestUtils.clientPost(context, URLManage.getInstance().villagehealthport
                        , stringEntity, new TextHttpResponseHandler() {
                            @Override
                            public void onFailure(int statusCode,
                                    org.apache.http.Header[] headers,
                                    final String responseString, Throwable
                                    throwable) {
                                measureDataBean.setUploadFlag(false);
                                DBUtitl.updataMeasure(context, measureDataBean);
                                DBDataUtil.updatePatientUploadState(context
                                        , measureDataBean.getIdcard());
                                UiUtils.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        responseCallBack.onFailure(responseString);
                                        ToastUtils.showShortToast(R.string.upload_failure);
                                    }
                                });
                            }

                            @Override
                            public void onSuccess(int statusCode,
                                    org.apache.http.Header[] headers,
                                    final String responseString) {
                                UiUtils.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            BaseResponse response = JsonUtil.toObject(
                                                    responseString, BaseResponse.class);
                                            responseCallBack.onSuccess(response.msgCode);
                                        } catch (Exception e) {
                                            responseCallBack.onFailure("");
                                            ToastUtils.showShortToast(R.string.upload_failure);
                                        }
                                    }
                                });
                            }
                        });
                //上传数据到云平台
                UploadCloudMeasureData.newInstance().uploadPhysicalMeasure(context, patientBean
                        , measureDataBean);
            } catch (Exception e) {
                e.printStackTrace();
                measureDataBean.setUploadFlag(false);
                DBUtitl.updataMeasure(context, measureDataBean);
                DBDataUtil.updatePatientUploadState(context
                        , measureDataBean.getIdcard());
                UiUtils.post(new Runnable() {
                    @Override
                    public void run() {
                        UiUtils.post(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtils.showShortToast(R.string.upload_failure);
                            }
                        });
                        responseCallBack.onException();
                    }
                });
            }
        }
    }

    /**
     * 远程心电申请
     * @param request 请求参数
     * @param responseCallBack 结果回调
     */
    public void requestEcgApply(final EcgDiagnoseApplyRequest request
            , final ResponseCallBack responseCallBack) {
        this.responseCallBack = responseCallBack;
        UiUtils.postShortThread(new Runnable() {
            @Override
            public void run() {
                try {
                    StringEntity stringEntity = new StringEntity(JsonUtil.toJson(request));
                    RequestUtils.clientPost(context, URLManage.getInstance().requestEcgDiagnose
                            , stringEntity, new TextHttpResponseHandler() {
                                @Override
                                public void onFailure(int statusCode, org.apache.http.Header[]
                                        headers, final String s, Throwable throwable) {
                                    UiUtils.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            responseCallBack.onFailure(s);
                                        }
                                    });
                                }

                                @Override
                                public void onSuccess(int statusCode,
                                        org.apache.http.Header[] headers, final String s) {
                                    UiUtils.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                if (s.length() > 0) {
                                                    BaseResponse response = null;

                                                    response = JsonUtil.toObject(s,
                                                            BaseResponse.class);

                                                    String message = response.msgText;
                                                    if (response.msgCode.equals("10000")) {
                                                        //申请成功
                                                        responseCallBack.onSuccess(message);
                                                    } else {
                                                        //申请失败
                                                        responseCallBack.onFailure(message);
                                                    }
                                                }
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                                }
                            });
                } catch (Exception e) {
                    e.printStackTrace();
                    UiUtils.post(new Runnable() {
                        @Override
                        public void run() {
                            responseCallBack.onException();
                        }
                    });
                }
            }
        });
    }

    /**
     * 远程心电查询
     * @param request 请求参数
     * @param responseCallBack 结果回调
     */
    public void queryEcgRemote(final QueryEcgDiagnosesRequest request
            , final ResponseCallBack responseCallBack) {
        this.responseCallBack = responseCallBack;
        UiUtils.postLongThread(new Runnable() {
            @Override
            public void run() {
                try {
                    StringEntity stringEntity = new StringEntity(JsonUtil
                            .toJson(request), textStyle);
                    //打印请求参数，不关闭该打印
                    RequestUtils.clientPost(context, URLManage.getInstance().queryEcgDiagnoses
                            , stringEntity, new TextHttpResponseHandler() {
                                @Override
                                public void onFailure(int statusCode, org.apache.http.Header[]
                                        headers, final String s, Throwable throwable) {
                                    UiUtils.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            responseCallBack.onFailure(s);
                                        }
                                    });
                                }

                                @Override
                                public void onSuccess(int statusCode,
                                        org.apache.http.Header[] headers, final String s) {
                                    UiUtils.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                if (s.length() > 0) {
                                                    BaseResponse response = null;

                                                    response = JsonUtil.toObject(s,
                                                            BaseResponse.class);

                                                    String message = response.msgText;
                                                    if (response.msgCode.equals("10000")) {
                                                        //申请成功
                                                        responseCallBack.onSuccess(message);
                                                    } else {
                                                        //申请失败
                                                        responseCallBack.onFailure(message);
                                                    }
                                                }
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                                }
                            });
                } catch (Exception e) {
                    e.printStackTrace();
                    UiUtils.post(new Runnable() {
                        @Override
                        public void run() {
                            responseCallBack.onException();
                        }
                    });
                }
            }
        });
    }

    /**
     * 多条测量数据上传
     * @param measureDataBean 测量bean类
     * @param patientBean 病人实体类
     * @param checkData 医生
     * @param isHint 是否需要提示
     * @return 检查是否有建档信息
     */
    public boolean uploadMeasureDataMore(final MeasureDataBean measureDataBean
            , final PatientBean patientBean, CheckData checkData, final boolean isHint) {
        EcgRemoteInfoSaveModule.getInstance().isUploadSuccess = false;
        if (UiUtils.isShuYangArea()) {
            if (!patientBean.getIsFileCreat()) {
                //针对标记建档的居民时，先查询该居民是否建档，如果已经建档则标记该居民已经建立档案，并且上传数据，如果未建档，则标记未建档，不上传数据
                final PatientBeanDao patientDao = GreenDaoManager
                        .getPatientDao(context);
                QueryHealthFile healthFile = new QueryHealthFile();
                healthFile.setIdnumber(patientBean.getIdcard());
                healthFile.setSelfname(patientBean.getName());
                try {
                    StringEntity stringEntity = new StringEntity(JsonUtil
                            .toJson(healthFile), "utf-8");
                    RequestUtils.clientPost(MyApplication.getContext(),
                            URLManage.getInstance().healthfileport,
                            stringEntity, new TextHttpResponseHandler() {

                                @Override
                                public void onFailure(int statusCode, Header[] headers,
                                        String responseString, Throwable throwable) {

                                }

                                @Override
                                public void onSuccess(int statusCode, Header[] headers,
                                        String responseString) {
                                    patientBean.setIsOtherNetSuccess(true);
                                    if (responseString != null) {
                                        JsonObject jsonObject = new JsonParser()
                                                .parse(responseString)
                                                .getAsJsonObject();
                                        int resultCode = jsonObject.get("resultCode")
                                                .getAsInt();
                                        if (resultCode == GlobalConstant.HTTP_SUCCESS) {
                                            //有档案
                                            patientBean.setIsFileCreat(true);
                                        } else if (resultCode == GlobalConstant
                                                .HTTP_NO_HAVE) {
                                            //没有档案

                                        } else {
                                            //请求失败
                                            patientBean.setIsOtherNetSuccess(false);
                                        }
                                        patientDao.update(patientBean);
                                        BroadcastUtils.sendRefrshPatient();
                                    }
                                }
                            });
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            //查询之后没建档，不上传数据
            if (!patientBean.getIsFileCreat()) {
                return false;
            }
        }
        //上传数据到云平台
        UploadCloudMeasureData.newInstance().uploadPhysicalMeasure(context,
                patientBean, measureDataBean);
        final VillageHealthPortRequest vhpr = new VillageHealthPortRequest();
        fillData(vhpr, measureDataBean, patientBean, checkData);
        try {
            StringEntity stringEntity = new StringEntity(JsonUtil.toJson(vhpr),
                    textStyle);
            RequestUtils.clientPost(context, URLManage.getInstance().villagehealthport
                    , stringEntity, new TextHttpResponseHandler() {
                        @Override
                        public void onFailure(int statusCode,
                                org.apache.http.Header[] headers,
                                String s, Throwable throwable) {
                            Log.e("xsc_failure", "failure");
                            measureDataBean.setUploadFlag(false);
                            DBUtitl.updataMeasure(context, measureDataBean);
                            DBDataUtil.updatePatientUploadState(context
                                    , measureDataBean.getIdcard());
                        }

                        @Override
                        public void onSuccess(int statusCode,
                                org.apache.http.Header[] headers, String s) {
                            Log.e("xsc_success", "success");
                            try {
                                BaseResponse response = null;
                                response = JsonUtil.toObject(s,
                                        BaseResponse.class);
                                if (response.msgCode.equals("10000")) {
                                    EcgRemoteInfoSaveModule.getInstance()
                                            .isUploadSuccess = true;
                                    measureDataBean.setUploadFlag(true);
                                    DBUtitl.updataMeasure(context, measureDataBean);
                                    DBDataUtil.updatePatientUploadState(context
                                            , measureDataBean.getIdcard());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
        } catch (Exception e) {
            Log.e("xsc_exception", "exception");
            e.printStackTrace();
            measureDataBean.setUploadFlag(false);
            DBUtitl.updataMeasure(context, measureDataBean);
            DBDataUtil.updatePatientUploadState(context
                    , measureDataBean.getIdcard());
        }
        return true;
    }

    /**
     * 请求参数赋值
     * @param vhpr 参数请求类
     * @param measureDataBean 测量类
     * @param patientBean 病人信息类
     * @param checkData 当前医生信息
     */
    private void fillData(VillageHealthPortRequest vhpr, MeasureDataBean measureDataBean
            , PatientBean patientBean, CheckData checkData) {
        long l = System.currentTimeMillis();
        String deviceID = ProviderReader.getDeviceCode(context);
        if (TextUtils.isEmpty(deviceID)) {
            deviceID = "M0000";
        }
        long l2 = System.currentTimeMillis();
        //检查时间yyyy-MM-dd hh:mm:ss
        String measureTime = UiUtils.getDateFormat(UiUtils.DateState.LONG)
                .format(new Date(measureDataBean.getMeasureTime()));
        String uploadData = UiUtils.getDateFormat(UiUtils.DateState.LONG).format(new Date());
        //事先固定好的一个私钥
        String single = "konsung";
        //加密字段，key=md5(uploadDate+area+single),其中single是在事先固定好的一个私钥。
        String key = UiUtils.stringMD5(uploadData + UiUtils.getAreaName() + single);
        String sex = patientBean.getSex() + "";
        //体温 摄氏度
        int tempValue = measureDataBean.getTrendValue(KParamType.IRTEMP_TREND);
        String temp = tempValue == GlobalConstant.INVALID_DATA ? "" :
                String.valueOf(tempValue / GlobalConstant.FACTOR);
        //基本信息
        vhpr.id = measureDataBean.getUuid();
        vhpr.doctorCode = checkData.getCheckData().getUserName();
        vhpr.empId = checkData.getCheckData().getEmpId();
        vhpr.orgCode = checkData.getCheckData().getOrgId();
        vhpr.deviceCode = deviceID;
        vhpr.uploadDate = uploadData;
        vhpr.checkDate = measureTime;
        vhpr.area = UiUtils.getAreaName();
        vhpr.key = key;
        vhpr.patientType = patientBean.getPatientType() + "";
        vhpr.version = GlobalConstant.INTEFACE_VERSION;
        vhpr.deviceVersion = GlobalConstant.INTEFACE_VERSION;
        vhpr.equipmentModel = UiUtils.getAreaName() + "-1";
        vhpr.height = measureDataBean.getHeight() == GlobalConstant.INVALID_DATA ? ""
                : String.valueOf(measureDataBean.getHeight() / GlobalConstant.FACTOR);
        vhpr.weight = measureDataBean.getWeight() == GlobalConstant.INVALID_DATA ? ""
                : String.valueOf(measureDataBean.getWeight() / GlobalConstant.FACTOR);
        vhpr.bmi = UiUtils.countBmi(vhpr.height, vhpr.weight);
        vhpr.waist = patientBean.getWaist() == null ? "" : patientBean.getWaist();
        vhpr.hipline = patientBean.getHipline() == null ? "" : patientBean.getHipline();
        //病人信息
        vhpr.personinfo.birthDay = UiUtils.getDateFormat(UiUtils.DateState.SHORT).
                format(new Date(patientBean.getBirthday()));
        vhpr.personinfo.cardNo = patientBean.getIdcard();
        vhpr.personinfo.name = patientBean.getName();
        vhpr.personinfo.sex = sex;
        vhpr.personinfo.telePhone = patientBean.getContact();
        vhpr.personinfo.age = patientBean.getAge() + "";
        vhpr.personinfo.address = patientBean.getAddress();
        if (!TextUtils.isEmpty(patientBean.getBmpStr())) {
            vhpr.personinfo.headPortrait
                    = BmpUtil.bmp2bytes(BmpUtil.getBitmapByFileName(patientBean.getBmpStr()));
        }

        int blood = patientBean.getBlood();
        //后台字典9为不详
        if (blood == 0) {
            vhpr.personinfo.bloodType = "9";
        } else {
            vhpr.personinfo.bloodType = blood + "";
        }

        //心电数据
        vhpr.uwd.hr = (measureDataBean.getTrendValue(KParamType.ECG_HR)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf(measureDataBean.getTrendValue(KParamType
                .ECG_HR) / GlobalConstant.INT_FACTOR);
        vhpr.uwd.resp_rr = (measureDataBean.getTrendValue(KParamType.RESP_RR) ==
                GlobalConstant.INVALID_DATA) ? "" : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.RESP_RR) / GlobalConstant.ECG_TIME);
        vhpr.uwd.sample = "500";
        vhpr.uwd.p05 = "2150";
        vhpr.uwd.n05 = "1946";
        vhpr.uwd.duration = "5";

        // PR间期
        vhpr.uwd.PR = String.valueOf(measureDataBean.getPr());
        // QRS间期, 单位ms
        vhpr.uwd.QRS = String.valueOf(measureDataBean.getQrs());
        // QT间期
        vhpr.uwd.QT = String.valueOf(measureDataBean.getQt());
        // QTC间期
        vhpr.uwd.QTC = String.valueOf(measureDataBean.getQtc());
        // P 波轴
        vhpr.uwd.P = String.valueOf(measureDataBean.getPAxis());
        // QRS波心电轴
        vhpr.uwd.QRSZ = String.valueOf(measureDataBean.getQrsAxis());
        // T波心电轴
        vhpr.uwd.T = String.valueOf(measureDataBean.getTAxis());
        // RV5, 单位0.01ms
        vhpr.uwd.RV5 = measureDataBean.getRv5() / GlobalConstant.FACTOR + "";
        // SV1, 单位0.01ms
        vhpr.uwd.SV1 = measureDataBean.getSv1() / GlobalConstant.FACTOR + "";
        //RV5+SV1
        String rv5PlusSv1 = (measureDataBean.getSv1() + measureDataBean.getRv5())
                / GlobalConstant.FACTOR + "";
        //心电图自动诊断结果
        String anal = vhpr.uwd.hr + "," + vhpr.uwd.PR + "," + vhpr.uwd.QRS + "," + vhpr.uwd.QT +
                "," + vhpr.uwd.QTC + "," + vhpr.uwd.P + "," + vhpr.uwd.QRSZ + "," + vhpr.uwd.T +
                "," + vhpr.uwd.RV5 + "," + vhpr.uwd.SV1 + "," + "" + rv5PlusSv1 + ",,,,,," +
                measureDataBean.getEcgDiagnoseResult();
        vhpr.uwd.anal = anal;

        vhpr.uwd.ecg_i = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_I))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_ii = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_II))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_iii = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_III))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_avr = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_AVR))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_avf = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_AVF))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_avl = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_AVL))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_v1 = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_V1))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_v2 = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_V2))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_v3 = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_V3))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_v4 = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_V4))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_v5 = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_V5))
                , Base64.NO_WRAP);
        vhpr.uwd.ecg_v6 = Base64.encodeToString(UnitConvertUtil
                        .getByteformHexString(measureDataBean.getEcgWave(KParamType.ECG_V6))
                , Base64.NO_WRAP);
        //血糖数据，默认取餐前，餐前无效值取餐后
        int glu = measureDataBean.getTrendValue(KParamType.BLOODGLU_BEFORE_MEAL);
        glu = glu == GlobalConstant.INVALID_DATA ?
                measureDataBean.getTrendValue(KParamType.BLOODGLU_AFTER_MEAL) : glu;
        vhpr.ubsud.glu = glu == GlobalConstant.INVALID_DATA ? ""
                : String.valueOf((float) glu / GlobalConstant.INT_FACTOR);

        vhpr.ubsud.gluStyle = measureDataBean.getGluStyle();
        vhpr.ubsud.bsPh = (measureDataBean.getTrendValue(KParamType.URICACID_TREND)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.URICACID_TREND) / GlobalConstant.INT_FACTOR);
        vhpr.ubsud.totalCholesterol = MeasureUtil.measureValueShowUi2(KParamType.CHOLESTEROL_TREND,
                measureDataBean.getTrendValue(KParamType.CHOLESTEROL_TREND));
        //血氧
        vhpr.uod.spo2 = (measureDataBean.getTrendValue(KParamType.SPO2_TREND) ==
                GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf(measureDataBean.getTrendValue(KParamType
                .SPO2_TREND) / GlobalConstant.INT_FACTOR);
        vhpr.uod.oxPr = (measureDataBean.getTrendValue(KParamType.SPO2_PR)
                == GlobalConstant.INVALID_DATA) ? "" :
                String.valueOf(measureDataBean.getTrendValue(KParamType.SPO2_PR) /
                        GlobalConstant.INT_FACTOR);
        //血压
        vhpr.ubpd.sbp = (measureDataBean.getTrendValue(KParamType.NIBP_SYS)
                == GlobalConstant.INVALID_DATA) ?
                "" : String.valueOf(measureDataBean.getTrendValue(KParamType.NIBP_SYS) /
                GlobalConstant.INT_FACTOR);
        vhpr.ubpd.dbp = (measureDataBean.getTrendValue(KParamType.NIBP_DIA)
                == GlobalConstant.INVALID_DATA) ?
                "" : String.valueOf(measureDataBean.getTrendValue(KParamType.NIBP_DIA) /
                GlobalConstant.INT_FACTOR);
        vhpr.ubpd.bpPr = (measureDataBean.getTrendValue(KParamType.NIBP_PR)
                == GlobalConstant.INVALID_DATA) ?
                "" : String.valueOf(measureDataBean.getTrendValue(KParamType.NIBP_PR) /
                GlobalConstant.INT_FACTOR);
        //体温
        vhpr.temp.temp = temp;
        //尿常规
        vhpr.uud.urinePh = MeasureUtil.measureValueShowUi2(KParamType.URINERT_PH,
                measureDataBean.getTrendValue(KParamType.URINERT_PH));
        vhpr.uud.urineUbg = MeasureUtil.measureValueShowUi2(KParamType.URINERT_UBG,
                measureDataBean.getTrendValue(KParamType.URINERT_UBG));
        vhpr.uud.urineBld = MeasureUtil.measureValueShowUi2(KParamType.URINERT_BLD,
                measureDataBean.getTrendValue(KParamType.URINERT_BLD));
        vhpr.uud.urinePro = MeasureUtil.measureValueShowUi2(KParamType.URINERT_PRO,
                measureDataBean.getTrendValue(KParamType.URINERT_PRO));
        vhpr.uud.urineKet = MeasureUtil.measureValueShowUi2(KParamType.URINERT_KET,
                measureDataBean.getTrendValue(KParamType.URINERT_KET));
        vhpr.uud.urineNit = MeasureUtil.measureValueShowUi2(KParamType.URINERT_NIT,
                measureDataBean.getTrendValue(KParamType.URINERT_NIT));
        vhpr.uud.urineGlu = MeasureUtil.measureValueShowUi2(KParamType.URINERT_GLU,
                measureDataBean.getTrendValue(KParamType.URINERT_GLU));
        vhpr.uud.urineBil = MeasureUtil.measureValueShowUi2(KParamType.URINERT_BIL,
                measureDataBean.getTrendValue(KParamType.URINERT_BIL));
        vhpr.uud.urineLeu = MeasureUtil.measureValueShowUi2(KParamType.URINERT_LEU,
                measureDataBean.getTrendValue(KParamType.URINERT_LEU));
        vhpr.uud.urineSg = MeasureUtil.measureValueShowUi2(KParamType.URINERT_SG,
                measureDataBean.getTrendValue(KParamType.URINERT_SG));
        vhpr.uud.urineVc = MeasureUtil.measureValueShowUi2(KParamType.URINERT_ASC,
                measureDataBean.getTrendValue(KParamType.URINERT_ASC));
        vhpr.uud.urineCre = MeasureUtil.measureValueShowUi2(KParamType.URINERT_CRE,
                measureDataBean.getTrendValue(KParamType.URINERT_CRE));
        vhpr.uud.urineCa = MeasureUtil.measureValueShowUi2(KParamType.URINERT_CA,
                measureDataBean.getTrendValue(KParamType.URINERT_CA));
        vhpr.uud.urineMa = MeasureUtil.measureValueShowUi2(KParamType.URINERT_ALB,
                measureDataBean.getTrendValue(KParamType.URINERT_ALB));
        //血红蛋白
        vhpr.uhd.hgb = (measureDataBean.getTrendValue(KParamType.BLOOD_HGB)
                == GlobalConstant.INVALID_DATA) ? "" :
                String.valueOf(measureDataBean.getTrendValue(KParamType.BLOOD_HGB)
                        / GlobalConstant.INT_FACTOR);
        vhpr.uhd.htc = (measureDataBean.getTrendValue(KParamType.BLOOD_HGB)
                == GlobalConstant.INVALID_DATA) ? "" :
                String.valueOf(measureDataBean.getTrendValue(KParamType.BLOOD_HCT)
                        / GlobalConstant.FACTOR);
        //血脂四项
        vhpr.ubsd.cholesterol = (measureDataBean.getTrendValue(KParamType.BLOOD_FAT_CHO)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.BLOOD_FAT_CHO) / GlobalConstant.INT_FACTOR);
        vhpr.ubsd.flipidsTrig = (measureDataBean.getTrendValue(KParamType.BLOOD_FAT_TRIG)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.BLOOD_FAT_TRIG) / GlobalConstant.INT_FACTOR);
        vhpr.ubsd.flipidsHdl = (measureDataBean.getTrendValue(KParamType.BLOOD_FAT_HDL)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.BLOOD_FAT_HDL) / GlobalConstant.INT_FACTOR);
        vhpr.ubsd.flipidsLDL = (measureDataBean.getTrendValue(KParamType.BLOOD_FAT_LDL)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.BLOOD_FAT_LDL) / GlobalConstant.INT_FACTOR);
        //糖化血红蛋白
        vhpr.ughd.hba1cNgsp = (measureDataBean.getTrendValue(KParamType.GHB_HBA1C_NGSP)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.GHB_HBA1C_NGSP) / GlobalConstant.INT_FACTOR);
        vhpr.ughd.hba1cIfcc = (measureDataBean.getTrendValue(KParamType.GHB_HBA1C_IFCC)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.GHB_HBA1C_IFCC) / GlobalConstant.INT_FACTOR);
        vhpr.ughd.hba1cEag = (measureDataBean.getTrendValue(KParamType.GHB_EAG)
                == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.GHB_EAG) / GlobalConstant.INT_FACTOR);
        //白细胞
        vhpr.hemameba.hemameba = (measureDataBean.getTrendValue(KParamType.BLOOD_WBC)
                == GlobalConstant.INVALID_DATA) ? "" : String.valueOf((float)
                measureDataBean.getTrendValue(KParamType.BLOOD_WBC) / GlobalConstant.WBC_FACTOR);
        //总胆固醇(单测项)
        vhpr.chol = (measureDataBean.getTrendValue(KParamType
                .CHOLESTEROL_TREND) == GlobalConstant.INVALID_DATA) ? ""
                : String.valueOf((float) measureDataBean
                .getTrendValue(KParamType.CHOLESTEROL_TREND) / GlobalConstant.INT_FACTOR);
    }
}
